package com.iretailer.report.influxdb.service;

import com.iretailer.report.dto.PassByModel;
import com.iretailer.report.influxdb.dao.InfluxdbDao;
import com.iretailer.report.influxdb.model.PassByFlow;
import com.iretailer.report.influxdb.util.TableName;
import com.iretailer.report.model.PhysicalZoneMall;
import com.iretailer.report.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Auther: xjw
 * @Date: 2019/3/14 14:40
 * @Description:
 */
@Service
public class InfluxdbPassByService {
    @Autowired
    private InfluxdbDao influxdbDao;

    public int insertPassBy(List<PassByFlow> post, TableName.PassByflow table) {
        int result = -1;
        int separateNumber = 50000;
        int size = post.size();
        if (size <= separateNumber) {
            result = influxdbDao.batchInsertPassbyFlow(post, table);
        } else {
            int batchSize = size / separateNumber;
            int remain = size % separateNumber;
            for (int i = 0; i < batchSize; ) {
                int index = i * separateNumber;
                int toIndex = index + separateNumber;
                result += influxdbDao.batchInsertPassbyFlow(post.subList(index, toIndex), table);
            }
            if (remain > 0) {
                result += influxdbDao.batchInsertPassbyFlow(post.subList(size - remain, size), table);
            }
        }
        return result;
    }

    public List<PassByFlow> getZero(String timeZone, TableName.PassByflow table, List<Long> zoneIds, String startTime, String endTime) {
        List<PassByFlow> oldFlow = selectPassByFlowGrid(timeZone, table, zoneIds, startTime, endTime);
        oldFlow.forEach(it -> {
            it.setCountEnter(0);
            it.setCountPass(0);
        });
        return oldFlow;
    }


    public List<PassByFlow> selectPassByFlowGrid(String timeZone, TableName.PassByflow table, List<Long> zoneIds, String startTime, String endTime) {
        return influxdbDao.selectPassByFlowGrid(timeZone, table, zoneIds, startTime, endTime);
    }

    public List<PassByFlow> selectGroupByZoneId(String timezone, TableName.PassByflow table, List<Long> zoneIds, String startTime, String endTime, String period) {
        return influxdbDao.selectPassByFlowGroupByZoneId(timezone, table, zoneIds, startTime, endTime, period);
    }

    public List<PassByFlow> select_week(String timeZone, TableName.PassByflow table, List<Long> zoneIds, String startTime, String endTime, String period, String offset) {
        return influxdbDao.selectPassByFlowGroup_offset_By_zoneid_Time(timeZone, table, zoneIds, startTime, endTime, period, offset);
    }


    public List<PassByModel> getPhysicalZones_PeriodPassByFlow(List<PhysicalZoneMall> physicalZones, String startTime, String endTime, String period) {

        List<PassByModel> result = new ArrayList<>();
        if (physicalZones != null && physicalZones.size() > 0) {
            Map<String, List<PhysicalZoneMall>> timezonePZMap = physicalZones.stream().collect(Collectors.groupingBy(PhysicalZoneMall::getTimeZone));
            timezonePZMap.entrySet().forEach(it -> {
                String timezone = it.getKey();
                Map<Long, PhysicalZoneMall> pzmap = it.getValue().stream().collect(Collectors.toMap(PhysicalZoneMall::getId, a -> a));
                List<PassByFlow> passByFlows = null;
                if (period.equals("7d")) {
                    passByFlows = select_week(timezone, TableName.PassByflow.Passbyflow, new ArrayList<>(pzmap.keySet()), startTime, endTime, "7d", "-3d");
                } else {
                    passByFlows = selectGroupByZoneId(timezone, TableName.PassByflow.Passbyflow, new ArrayList<>(pzmap.keySet()), startTime, endTime, period);
                }
                if (passByFlows != null && passByFlows.size() > 0) {
                    for (PassByFlow pbf : passByFlows) {
                        PhysicalZoneMall pzm = pzmap.get(Long.valueOf(pbf.getZoneid()));
                        PassByModel pbm = new PassByModel();
                        pbm.setPhysicalZoneId(String.valueOf(pzm.getId()));
                        pbm.setPhysicalZoneName(pzm.getName());
                        pbm.setMallId(pzm.getMallId());
                        pbm.setMallName(pzm.getMallName());
                        pbm.setCityId(pzm.getCityId());
                        pbm.setZonedDateTime(pbf.getTime());
                        String time = TimeUtil.zoneDateTime_format(pbf.getTime());
                        pbm.setDateTime(time);
                        pbm.setCountEnter(pbf.getCountEnter());
                        pbm.setCountPass(pbf.getCountPass());
                        result.add(pbm);
                    }
                }
            });
        }
        return result;
    }
}
